{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "d1f20875-c50d-46df-906b-49ec1d0c6002",
   "metadata": {},
   "source": [
    "## 9.2 使用Model类搭建神经网络实现鸢尾花分类\n"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "c90a18b9-d8e8-431c-856c-35f1e4ed05e1",
   "metadata": {},
   "source": [
    "### 1.任务描述\n",
    "\n",
    "搭建一层神经网络，实现鸢尾花分类。\n",
    "\n",
    "要求：\n",
    "- 使用Model类搭建神经网络\n",
    "- 网络结构：单层神经网络\n",
    "- 网络的输出层使用Softmax将线性输出转换为概率分布\n",
    "- 输出迭代过程的损失和准确率\n",
    "- 使用L2正则化缓解过拟合\n"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "f5b4fc39-cbcf-432a-bf1e-e75e642d4b87",
   "metadata": {},
   "source": [
    "### 2.知识准备\n",
    "\n",
    "见教程。"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "c1d0295a-4ac4-470a-8263-027a3d69ac2c",
   "metadata": {},
   "source": [
    "### 3.任务分析\n",
    "\n",
    "使用Model类搭建神经网络，可以采用六步法：\n",
    "- 导入相关模块\n",
    "- 指定输入网络的训练集和测试集，训练集特征x_train和标签y_train，测试集特征x_test和标签y_test\n",
    "- 自定义网络结构\n",
    "- 在model.compile方法中配置训练方法，选择在训练时使用的优化器、损失函数和最终评测指标\n",
    "- 在model.fit方法中执行训练过程，告知训练集和测试集的输入值和标签、每个batch 的大小（batch_size）和数据集的迭代次数（epochs）\n",
    "- 使用model.summary方法打印网络结构，统计参数数目"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "435c6090-cfda-4f46-a550-22a368e41e4a",
   "metadata": {},
   "source": [
    "### 4.任务实施\n"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "ec75eb6c-5da3-467d-a471-ca3b47242dd6",
   "metadata": {},
   "source": [
    "执行代码"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "2ae9da58-e339-4d22-9f8d-ca255711d89e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Epoch 1/300\n",
      "1/1 [==============================] - 1s 977ms/step - loss: 0.9055 - sparse_categorical_accuracy: 0.3750\n",
      "Epoch 2/300\n",
      "1/1 [==============================] - 0s 4ms/step - loss: 0.8455 - sparse_categorical_accuracy: 0.5083\n",
      "Epoch 3/300\n",
      "1/1 [==============================] - 0s 6ms/step - loss: 0.7929 - sparse_categorical_accuracy: 0.5083\n",
      "Epoch 4/300\n",
      "1/1 [==============================] - 0s 4ms/step - loss: 0.7508 - sparse_categorical_accuracy: 0.5333\n",
      "Epoch 5/300\n",
      "1/1 [==============================] - 0s 4ms/step - loss: 0.7150 - sparse_categorical_accuracy: 0.7083\n",
      "Epoch 6/300\n",
      "1/1 [==============================] - 0s 4ms/step - loss: 0.6849 - sparse_categorical_accuracy: 0.8167\n",
      "Epoch 7/300\n",
      "1/1 [==============================] - 0s 3ms/step - loss: 0.6587 - sparse_categorical_accuracy: 0.8667\n",
      "Epoch 8/300\n",
      "1/1 [==============================] - 0s 3ms/step - loss: 0.6362 - sparse_categorical_accuracy: 0.8667\n",
      "Epoch 9/300\n",
      "1/1 [==============================] - 0s 5ms/step - loss: 0.6167 - sparse_categorical_accuracy: 0.8750\n",
      "Epoch 10/300\n",
      "1/1 [==============================] - 0s 132ms/step - loss: 0.5997 - sparse_categorical_accuracy: 0.8833 - val_loss: 0.6925 - val_sparse_categorical_accuracy: 0.7667\n",
      "Epoch 11/300\n",
      "1/1 [==============================] - 0s 6ms/step - loss: 0.5849 - sparse_categorical_accuracy: 0.8833\n",
      "Epoch 12/300\n",
      "1/1 [==============================] - 0s 5ms/step - loss: 0.5718 - sparse_categorical_accuracy: 0.8833\n",
      "Epoch 13/300\n",
      "1/1 [==============================] - 0s 4ms/step - loss: 0.5604 - sparse_categorical_accuracy: 0.9000\n",
      "Epoch 14/300\n",
      "1/1 [==============================] - 0s 5ms/step - loss: 0.5500 - sparse_categorical_accuracy: 0.8917\n",
      "Epoch 15/300\n",
      "1/1 [==============================] - 0s 4ms/step - loss: 0.5410 - sparse_categorical_accuracy: 0.9000\n",
      "Epoch 16/300\n",
      "1/1 [==============================] - 0s 3ms/step - loss: 0.5327 - sparse_categorical_accuracy: 0.8917\n",
      "Epoch 17/300\n",
      "1/1 [==============================] - 0s 5ms/step - loss: 0.5255 - sparse_categorical_accuracy: 0.9000\n",
      "Epoch 18/300\n",
      "1/1 [==============================] - 0s 4ms/step - loss: 0.5188 - sparse_categorical_accuracy: 0.9000\n",
      "Epoch 19/300\n",
      "1/1 [==============================] - 0s 4ms/step - loss: 0.5127 - sparse_categorical_accuracy: 0.9000\n",
      "Epoch 20/300\n",
      "1/1 [==============================] - 0s 20ms/step - loss: 0.5071 - sparse_categorical_accuracy: 0.9000 - val_loss: 0.5808 - val_sparse_categorical_accuracy: 0.8667\n",
      "Epoch 21/300\n",
      "1/1 [==============================] - 0s 14ms/step - loss: 0.5020 - sparse_categorical_accuracy: 0.9000\n",
      "Epoch 22/300\n",
      "1/1 [==============================] - 0s 5ms/step - loss: 0.4974 - sparse_categorical_accuracy: 0.9000\n",
      "Epoch 23/300\n",
      "1/1 [==============================] - 0s 5ms/step - loss: 0.4931 - sparse_categorical_accuracy: 0.9000\n",
      "Epoch 24/300\n",
      "1/1 [==============================] - 0s 4ms/step - loss: 0.4892 - sparse_categorical_accuracy: 0.9000\n",
      "Epoch 25/300\n",
      "1/1 [==============================] - 0s 4ms/step - loss: 0.4856 - sparse_categorical_accuracy: 0.9000\n",
      "Epoch 26/300\n",
      "1/1 [==============================] - 0s 5ms/step - loss: 0.4822 - sparse_categorical_accuracy: 0.9000\n",
      "Epoch 27/300\n",
      "1/1 [==============================] - 0s 4ms/step - loss: 0.4790 - sparse_categorical_accuracy: 0.9000\n",
      "Epoch 28/300\n",
      "1/1 [==============================] - 0s 5ms/step - loss: 0.4760 - sparse_categorical_accuracy: 0.9000\n",
      "Epoch 29/300\n",
      "1/1 [==============================] - 0s 4ms/step - loss: 0.4733 - sparse_categorical_accuracy: 0.9083\n",
      "Epoch 30/300\n",
      "1/1 [==============================] - 0s 29ms/step - loss: 0.4706 - sparse_categorical_accuracy: 0.9000 - val_loss: 0.5308 - val_sparse_categorical_accuracy: 0.9000\n",
      "Epoch 31/300\n",
      "1/1 [==============================] - 0s 15ms/step - loss: 0.4682 - sparse_categorical_accuracy: 0.9083\n",
      "Epoch 32/300\n",
      "1/1 [==============================] - 0s 4ms/step - loss: 0.4659 - sparse_categorical_accuracy: 0.9000\n",
      "Epoch 33/300\n",
      "1/1 [==============================] - 0s 4ms/step - loss: 0.4638 - sparse_categorical_accuracy: 0.9083\n",
      "Epoch 34/300\n",
      "1/1 [==============================] - 0s 4ms/step - loss: 0.4617 - sparse_categorical_accuracy: 0.9000\n",
      "Epoch 35/300\n",
      "1/1 [==============================] - 0s 5ms/step - loss: 0.4599 - sparse_categorical_accuracy: 0.9167\n",
      "Epoch 36/300\n",
      "1/1 [==============================] - 0s 5ms/step - loss: 0.4580 - sparse_categorical_accuracy: 0.9000\n",
      "Epoch 37/300\n",
      "1/1 [==============================] - 0s 5ms/step - loss: 0.4564 - sparse_categorical_accuracy: 0.9167\n",
      "Epoch 38/300\n",
      "1/1 [==============================] - 0s 5ms/step - loss: 0.4549 - sparse_categorical_accuracy: 0.8917\n",
      "Epoch 39/300\n",
      "1/1 [==============================] - 0s 3ms/step - loss: 0.4534 - sparse_categorical_accuracy: 0.9333\n",
      "Epoch 40/300\n",
      "1/1 [==============================] - 0s 28ms/step - loss: 0.4520 - sparse_categorical_accuracy: 0.8917 - val_loss: 0.4948 - val_sparse_categorical_accuracy: 0.9333\n",
      "Epoch 41/300\n",
      "1/1 [==============================] - 0s 3ms/step - loss: 0.4507 - sparse_categorical_accuracy: 0.9333\n",
      "Epoch 42/300\n",
      "1/1 [==============================] - 0s 4ms/step - loss: 0.4497 - sparse_categorical_accuracy: 0.8917\n",
      "Epoch 43/300\n",
      "1/1 [==============================] - 0s 4ms/step - loss: 0.4487 - sparse_categorical_accuracy: 0.9417\n",
      "Epoch 44/300\n",
      "1/1 [==============================] - 0s 4ms/step - loss: 0.4481 - sparse_categorical_accuracy: 0.8750\n",
      "Epoch 45/300\n",
      "1/1 [==============================] - 0s 4ms/step - loss: 0.4473 - sparse_categorical_accuracy: 0.9667\n",
      "Epoch 46/300\n",
      "1/1 [==============================] - 0s 4ms/step - loss: 0.4471 - sparse_categorical_accuracy: 0.8667\n",
      "Epoch 47/300\n",
      "1/1 [==============================] - 0s 3ms/step - loss: 0.4468 - sparse_categorical_accuracy: 0.9667\n",
      "Epoch 48/300\n",
      "1/1 [==============================] - 0s 4ms/step - loss: 0.4477 - sparse_categorical_accuracy: 0.8667\n",
      "Epoch 49/300\n",
      "1/1 [==============================] - 0s 3ms/step - loss: 0.4473 - sparse_categorical_accuracy: 0.9583\n",
      "Epoch 50/300\n",
      "1/1 [==============================] - 0s 27ms/step - loss: 0.4489 - sparse_categorical_accuracy: 0.8667 - val_loss: 0.4548 - val_sparse_categorical_accuracy: 1.0000\n",
      "Epoch 51/300\n",
      "1/1 [==============================] - 0s 7ms/step - loss: 0.4488 - sparse_categorical_accuracy: 0.9500\n",
      "Epoch 52/300\n",
      "1/1 [==============================] - 0s 5ms/step - loss: 0.4524 - sparse_categorical_accuracy: 0.8500\n",
      "Epoch 53/300\n",
      "1/1 [==============================] - 0s 4ms/step - loss: 0.4520 - sparse_categorical_accuracy: 0.9333\n",
      "Epoch 54/300\n",
      "1/1 [==============================] - 0s 4ms/step - loss: 0.4577 - sparse_categorical_accuracy: 0.8333\n",
      "Epoch 55/300\n",
      "1/1 [==============================] - 0s 4ms/step - loss: 0.4560 - sparse_categorical_accuracy: 0.9250\n",
      "Epoch 56/300\n",
      "1/1 [==============================] - 0s 4ms/step - loss: 0.4640 - sparse_categorical_accuracy: 0.8000\n",
      "Epoch 57/300\n",
      "1/1 [==============================] - 0s 5ms/step - loss: 0.4605 - sparse_categorical_accuracy: 0.8917\n",
      "Epoch 58/300\n",
      "1/1 [==============================] - 0s 4ms/step - loss: 0.4709 - sparse_categorical_accuracy: 0.7917\n",
      "Epoch 59/300\n",
      "1/1 [==============================] - 0s 4ms/step - loss: 0.4646 - sparse_categorical_accuracy: 0.8500\n",
      "Epoch 60/300\n",
      "1/1 [==============================] - 0s 20ms/step - loss: 0.4774 - sparse_categorical_accuracy: 0.7583 - val_loss: 0.4278 - val_sparse_categorical_accuracy: 0.9333\n",
      "Epoch 61/300\n",
      "1/1 [==============================] - 0s 12ms/step - loss: 0.4674 - sparse_categorical_accuracy: 0.8500\n",
      "Epoch 62/300\n",
      "1/1 [==============================] - 0s 4ms/step - loss: 0.4817 - sparse_categorical_accuracy: 0.7417\n",
      "Epoch 63/300\n",
      "1/1 [==============================] - 0s 6ms/step - loss: 0.4691 - sparse_categorical_accuracy: 0.8417\n",
      "Epoch 64/300\n",
      "1/1 [==============================] - 0s 5ms/step - loss: 0.4840 - sparse_categorical_accuracy: 0.7417\n",
      "Epoch 65/300\n",
      "1/1 [==============================] - 0s 4ms/step - loss: 0.4691 - sparse_categorical_accuracy: 0.8417\n",
      "Epoch 66/300\n",
      "1/1 [==============================] - 0s 4ms/step - loss: 0.4843 - sparse_categorical_accuracy: 0.7417\n",
      "Epoch 67/300\n",
      "1/1 [==============================] - 0s 4ms/step - loss: 0.4680 - sparse_categorical_accuracy: 0.8417\n",
      "Epoch 68/300\n",
      "1/1 [==============================] - 0s 4ms/step - loss: 0.4831 - sparse_categorical_accuracy: 0.7417\n",
      "Epoch 69/300\n",
      "1/1 [==============================] - 0s 4ms/step - loss: 0.4668 - sparse_categorical_accuracy: 0.8583\n",
      "Epoch 70/300\n",
      "1/1 [==============================] - 0s 26ms/step - loss: 0.4818 - sparse_categorical_accuracy: 0.7417 - val_loss: 0.4175 - val_sparse_categorical_accuracy: 0.9333\n",
      "Epoch 71/300\n",
      "1/1 [==============================] - 0s 8ms/step - loss: 0.4653 - sparse_categorical_accuracy: 0.8583\n",
      "Epoch 72/300\n",
      "1/1 [==============================] - 0s 3ms/step - loss: 0.4803 - sparse_categorical_accuracy: 0.7417\n",
      "Epoch 73/300\n",
      "1/1 [==============================] - 0s 4ms/step - loss: 0.4639 - sparse_categorical_accuracy: 0.8583\n",
      "Epoch 74/300\n",
      "1/1 [==============================] - 0s 3ms/step - loss: 0.4783 - sparse_categorical_accuracy: 0.7417\n",
      "Epoch 75/300\n",
      "1/1 [==============================] - 0s 4ms/step - loss: 0.4614 - sparse_categorical_accuracy: 0.8500\n",
      "Epoch 76/300\n",
      "1/1 [==============================] - 0s 4ms/step - loss: 0.4751 - sparse_categorical_accuracy: 0.7583\n",
      "Epoch 77/300\n",
      "1/1 [==============================] - 0s 4ms/step - loss: 0.4597 - sparse_categorical_accuracy: 0.8500\n",
      "Epoch 78/300\n",
      "1/1 [==============================] - 0s 3ms/step - loss: 0.4735 - sparse_categorical_accuracy: 0.7667\n",
      "Epoch 79/300\n",
      "1/1 [==============================] - 0s 4ms/step - loss: 0.4575 - sparse_categorical_accuracy: 0.8500\n",
      "Epoch 80/300\n",
      "1/1 [==============================] - 0s 20ms/step - loss: 0.4702 - sparse_categorical_accuracy: 0.7667 - val_loss: 0.4107 - val_sparse_categorical_accuracy: 0.9333\n",
      "Epoch 81/300\n",
      "1/1 [==============================] - 0s 13ms/step - loss: 0.4553 - sparse_categorical_accuracy: 0.8500\n",
      "Epoch 82/300\n",
      "1/1 [==============================] - 0s 3ms/step - loss: 0.4672 - sparse_categorical_accuracy: 0.7750\n",
      "Epoch 83/300\n",
      "1/1 [==============================] - 0s 4ms/step - loss: 0.4532 - sparse_categorical_accuracy: 0.8500\n",
      "Epoch 84/300\n",
      "1/1 [==============================] - 0s 4ms/step - loss: 0.4650 - sparse_categorical_accuracy: 0.8000\n",
      "Epoch 85/300\n",
      "1/1 [==============================] - 0s 3ms/step - loss: 0.4512 - sparse_categorical_accuracy: 0.8500\n",
      "Epoch 86/300\n",
      "1/1 [==============================] - 0s 4ms/step - loss: 0.4622 - sparse_categorical_accuracy: 0.8000\n",
      "Epoch 87/300\n",
      "1/1 [==============================] - 0s 3ms/step - loss: 0.4490 - sparse_categorical_accuracy: 0.8583\n",
      "Epoch 88/300\n",
      "1/1 [==============================] - 0s 4ms/step - loss: 0.4598 - sparse_categorical_accuracy: 0.8000\n",
      "Epoch 89/300\n",
      "1/1 [==============================] - 0s 3ms/step - loss: 0.4470 - sparse_categorical_accuracy: 0.8583\n",
      "Epoch 90/300\n",
      "1/1 [==============================] - 0s 27ms/step - loss: 0.4571 - sparse_categorical_accuracy: 0.8000 - val_loss: 0.4053 - val_sparse_categorical_accuracy: 0.9333\n",
      "Epoch 91/300\n",
      "1/1 [==============================] - 0s 5ms/step - loss: 0.4454 - sparse_categorical_accuracy: 0.8583\n",
      "Epoch 92/300\n",
      "1/1 [==============================] - 0s 4ms/step - loss: 0.4552 - sparse_categorical_accuracy: 0.8000\n",
      "Epoch 93/300\n",
      "1/1 [==============================] - 0s 3ms/step - loss: 0.4434 - sparse_categorical_accuracy: 0.8667\n",
      "Epoch 94/300\n",
      "1/1 [==============================] - 0s 4ms/step - loss: 0.4528 - sparse_categorical_accuracy: 0.8083\n",
      "Epoch 95/300\n",
      "1/1 [==============================] - 0s 4ms/step - loss: 0.4415 - sparse_categorical_accuracy: 0.8667\n",
      "Epoch 96/300\n",
      "1/1 [==============================] - 0s 4ms/step - loss: 0.4502 - sparse_categorical_accuracy: 0.8083\n",
      "Epoch 97/300\n",
      "1/1 [==============================] - 0s 4ms/step - loss: 0.4393 - sparse_categorical_accuracy: 0.8667\n",
      "Epoch 98/300\n",
      "1/1 [==============================] - 0s 3ms/step - loss: 0.4476 - sparse_categorical_accuracy: 0.8250\n",
      "Epoch 99/300\n",
      "1/1 [==============================] - 0s 4ms/step - loss: 0.4381 - sparse_categorical_accuracy: 0.8667\n",
      "Epoch 100/300\n",
      "1/1 [==============================] - 0s 26ms/step - loss: 0.4464 - sparse_categorical_accuracy: 0.8333 - val_loss: 0.4008 - val_sparse_categorical_accuracy: 0.9333\n",
      "Epoch 101/300\n",
      "1/1 [==============================] - 0s 7ms/step - loss: 0.4364 - sparse_categorical_accuracy: 0.8667\n",
      "Epoch 102/300\n",
      "1/1 [==============================] - 0s 3ms/step - loss: 0.4441 - sparse_categorical_accuracy: 0.8333\n",
      "Epoch 103/300\n",
      "1/1 [==============================] - 0s 3ms/step - loss: 0.4348 - sparse_categorical_accuracy: 0.8667\n",
      "Epoch 104/300\n",
      "1/1 [==============================] - 0s 4ms/step - loss: 0.4421 - sparse_categorical_accuracy: 0.8333\n",
      "Epoch 105/300\n",
      "1/1 [==============================] - 0s 3ms/step - loss: 0.4330 - sparse_categorical_accuracy: 0.8833\n",
      "Epoch 106/300\n",
      "1/1 [==============================] - 0s 3ms/step - loss: 0.4401 - sparse_categorical_accuracy: 0.8417\n",
      "Epoch 107/300\n",
      "1/1 [==============================] - 0s 4ms/step - loss: 0.4312 - sparse_categorical_accuracy: 0.9083\n",
      "Epoch 108/300\n",
      "1/1 [==============================] - 0s 3ms/step - loss: 0.4375 - sparse_categorical_accuracy: 0.8417\n",
      "Epoch 109/300\n",
      "1/1 [==============================] - 0s 6ms/step - loss: 0.4291 - sparse_categorical_accuracy: 0.9167\n",
      "Epoch 110/300\n",
      "1/1 [==============================] - 0s 29ms/step - loss: 0.4353 - sparse_categorical_accuracy: 0.8417 - val_loss: 0.3975 - val_sparse_categorical_accuracy: 0.9667\n",
      "Epoch 111/300\n",
      "1/1 [==============================] - 0s 4ms/step - loss: 0.4277 - sparse_categorical_accuracy: 0.9167\n",
      "Epoch 112/300\n",
      "1/1 [==============================] - 0s 3ms/step - loss: 0.4335 - sparse_categorical_accuracy: 0.8417\n",
      "Epoch 113/300\n",
      "1/1 [==============================] - 0s 3ms/step - loss: 0.4263 - sparse_categorical_accuracy: 0.9167\n",
      "Epoch 114/300\n",
      "1/1 [==============================] - 0s 4ms/step - loss: 0.4317 - sparse_categorical_accuracy: 0.8417\n",
      "Epoch 115/300\n",
      "1/1 [==============================] - 0s 3ms/step - loss: 0.4245 - sparse_categorical_accuracy: 0.9167\n",
      "Epoch 116/300\n",
      "1/1 [==============================] - 0s 3ms/step - loss: 0.4296 - sparse_categorical_accuracy: 0.8500\n",
      "Epoch 117/300\n",
      "1/1 [==============================] - 0s 4ms/step - loss: 0.4228 - sparse_categorical_accuracy: 0.9250\n",
      "Epoch 118/300\n",
      "1/1 [==============================] - 0s 3ms/step - loss: 0.4279 - sparse_categorical_accuracy: 0.8500\n",
      "Epoch 119/300\n",
      "1/1 [==============================] - 0s 4ms/step - loss: 0.4216 - sparse_categorical_accuracy: 0.9333\n",
      "Epoch 120/300\n",
      "1/1 [==============================] - 0s 26ms/step - loss: 0.4260 - sparse_categorical_accuracy: 0.8500 - val_loss: 0.3948 - val_sparse_categorical_accuracy: 0.9667\n",
      "Epoch 121/300\n",
      "1/1 [==============================] - 0s 7ms/step - loss: 0.4202 - sparse_categorical_accuracy: 0.9333\n",
      "Epoch 122/300\n",
      "1/1 [==============================] - 0s 4ms/step - loss: 0.4249 - sparse_categorical_accuracy: 0.8500\n",
      "Epoch 123/300\n",
      "1/1 [==============================] - 0s 3ms/step - loss: 0.4191 - sparse_categorical_accuracy: 0.9333\n",
      "Epoch 124/300\n",
      "1/1 [==============================] - 0s 4ms/step - loss: 0.4234 - sparse_categorical_accuracy: 0.8500\n",
      "Epoch 125/300\n",
      "1/1 [==============================] - 0s 4ms/step - loss: 0.4178 - sparse_categorical_accuracy: 0.9333\n",
      "Epoch 126/300\n",
      "1/1 [==============================] - 0s 4ms/step - loss: 0.4212 - sparse_categorical_accuracy: 0.8583\n",
      "Epoch 127/300\n",
      "1/1 [==============================] - 0s 4ms/step - loss: 0.4156 - sparse_categorical_accuracy: 0.9333\n",
      "Epoch 128/300\n",
      "1/1 [==============================] - 0s 4ms/step - loss: 0.4191 - sparse_categorical_accuracy: 0.8583\n",
      "Epoch 129/300\n",
      "1/1 [==============================] - 0s 3ms/step - loss: 0.4140 - sparse_categorical_accuracy: 0.9333\n",
      "Epoch 130/300\n",
      "1/1 [==============================] - 0s 27ms/step - loss: 0.4169 - sparse_categorical_accuracy: 0.8667 - val_loss: 0.3934 - val_sparse_categorical_accuracy: 0.9667\n",
      "Epoch 131/300\n",
      "1/1 [==============================] - 0s 5ms/step - loss: 0.4123 - sparse_categorical_accuracy: 0.9333\n",
      "Epoch 132/300\n",
      "1/1 [==============================] - 0s 4ms/step - loss: 0.4153 - sparse_categorical_accuracy: 0.8667\n",
      "Epoch 133/300\n",
      "1/1 [==============================] - 0s 4ms/step - loss: 0.4111 - sparse_categorical_accuracy: 0.9333\n",
      "Epoch 134/300\n",
      "1/1 [==============================] - 0s 3ms/step - loss: 0.4136 - sparse_categorical_accuracy: 0.8667\n",
      "Epoch 135/300\n",
      "1/1 [==============================] - 0s 4ms/step - loss: 0.4094 - sparse_categorical_accuracy: 0.9417\n",
      "Epoch 136/300\n",
      "1/1 [==============================] - 0s 3ms/step - loss: 0.4115 - sparse_categorical_accuracy: 0.8667\n",
      "Epoch 137/300\n",
      "1/1 [==============================] - 0s 4ms/step - loss: 0.4077 - sparse_categorical_accuracy: 0.9417\n",
      "Epoch 138/300\n",
      "1/1 [==============================] - 0s 3ms/step - loss: 0.4095 - sparse_categorical_accuracy: 0.8667\n",
      "Epoch 139/300\n",
      "1/1 [==============================] - 0s 4ms/step - loss: 0.4062 - sparse_categorical_accuracy: 0.9417\n",
      "Epoch 140/300\n",
      "1/1 [==============================] - 0s 27ms/step - loss: 0.4080 - sparse_categorical_accuracy: 0.8750 - val_loss: 0.3930 - val_sparse_categorical_accuracy: 0.9667\n",
      "Epoch 141/300\n",
      "1/1 [==============================] - 0s 6ms/step - loss: 0.4050 - sparse_categorical_accuracy: 0.9417\n",
      "Epoch 142/300\n",
      "1/1 [==============================] - 0s 4ms/step - loss: 0.4066 - sparse_categorical_accuracy: 0.8750\n",
      "Epoch 143/300\n",
      "1/1 [==============================] - 0s 4ms/step - loss: 0.4038 - sparse_categorical_accuracy: 0.9500\n",
      "Epoch 144/300\n",
      "1/1 [==============================] - 0s 4ms/step - loss: 0.4051 - sparse_categorical_accuracy: 0.8750\n",
      "Epoch 145/300\n",
      "1/1 [==============================] - 0s 5ms/step - loss: 0.4024 - sparse_categorical_accuracy: 0.9500\n",
      "Epoch 146/300\n",
      "1/1 [==============================] - 0s 3ms/step - loss: 0.4037 - sparse_categorical_accuracy: 0.8750\n",
      "Epoch 147/300\n",
      "1/1 [==============================] - 0s 4ms/step - loss: 0.4013 - sparse_categorical_accuracy: 0.9500\n",
      "Epoch 148/300\n",
      "1/1 [==============================] - 0s 4ms/step - loss: 0.4024 - sparse_categorical_accuracy: 0.8750\n",
      "Epoch 149/300\n",
      "1/1 [==============================] - 0s 3ms/step - loss: 0.4001 - sparse_categorical_accuracy: 0.9500\n",
      "Epoch 150/300\n",
      "1/1 [==============================] - 0s 23ms/step - loss: 0.4008 - sparse_categorical_accuracy: 0.8917 - val_loss: 0.3932 - val_sparse_categorical_accuracy: 1.0000\n",
      "Epoch 151/300\n",
      "1/1 [==============================] - 0s 11ms/step - loss: 0.3988 - sparse_categorical_accuracy: 0.9500\n",
      "Epoch 152/300\n",
      "1/1 [==============================] - 0s 4ms/step - loss: 0.3997 - sparse_categorical_accuracy: 0.8917\n",
      "Epoch 153/300\n",
      "1/1 [==============================] - 0s 3ms/step - loss: 0.3980 - sparse_categorical_accuracy: 0.9500\n",
      "Epoch 154/300\n",
      "1/1 [==============================] - 0s 5ms/step - loss: 0.3987 - sparse_categorical_accuracy: 0.8917\n",
      "Epoch 155/300\n",
      "1/1 [==============================] - 0s 5ms/step - loss: 0.3969 - sparse_categorical_accuracy: 0.9500\n",
      "Epoch 156/300\n",
      "1/1 [==============================] - 0s 3ms/step - loss: 0.3974 - sparse_categorical_accuracy: 0.8917\n",
      "Epoch 157/300\n",
      "1/1 [==============================] - 0s 3ms/step - loss: 0.3955 - sparse_categorical_accuracy: 0.9583\n",
      "Epoch 158/300\n",
      "1/1 [==============================] - 0s 4ms/step - loss: 0.3960 - sparse_categorical_accuracy: 0.8917\n",
      "Epoch 159/300\n",
      "1/1 [==============================] - 0s 4ms/step - loss: 0.3947 - sparse_categorical_accuracy: 0.9583\n",
      "Epoch 160/300\n",
      "1/1 [==============================] - 0s 26ms/step - loss: 0.3951 - sparse_categorical_accuracy: 0.8917 - val_loss: 0.3940 - val_sparse_categorical_accuracy: 1.0000\n",
      "Epoch 161/300\n",
      "1/1 [==============================] - 0s 4ms/step - loss: 0.3936 - sparse_categorical_accuracy: 0.9583\n",
      "Epoch 162/300\n",
      "1/1 [==============================] - 0s 4ms/step - loss: 0.3938 - sparse_categorical_accuracy: 0.8917\n",
      "Epoch 163/300\n",
      "1/1 [==============================] - 0s 3ms/step - loss: 0.3925 - sparse_categorical_accuracy: 0.9583\n",
      "Epoch 164/300\n",
      "1/1 [==============================] - 0s 3ms/step - loss: 0.3928 - sparse_categorical_accuracy: 0.8917\n",
      "Epoch 165/300\n",
      "1/1 [==============================] - 0s 5ms/step - loss: 0.3916 - sparse_categorical_accuracy: 0.9583\n",
      "Epoch 166/300\n",
      "1/1 [==============================] - 0s 4ms/step - loss: 0.3916 - sparse_categorical_accuracy: 0.8917\n",
      "Epoch 167/300\n",
      "1/1 [==============================] - 0s 3ms/step - loss: 0.3906 - sparse_categorical_accuracy: 0.9583\n",
      "Epoch 168/300\n",
      "1/1 [==============================] - 0s 4ms/step - loss: 0.3906 - sparse_categorical_accuracy: 0.9083\n",
      "Epoch 169/300\n",
      "1/1 [==============================] - 0s 3ms/step - loss: 0.3896 - sparse_categorical_accuracy: 0.9667\n",
      "Epoch 170/300\n",
      "1/1 [==============================] - 0s 29ms/step - loss: 0.3896 - sparse_categorical_accuracy: 0.9083 - val_loss: 0.3961 - val_sparse_categorical_accuracy: 1.0000\n",
      "Epoch 171/300\n",
      "1/1 [==============================] - 0s 3ms/step - loss: 0.3888 - sparse_categorical_accuracy: 0.9667\n",
      "Epoch 172/300\n",
      "1/1 [==============================] - 0s 3ms/step - loss: 0.3888 - sparse_categorical_accuracy: 0.9083\n",
      "Epoch 173/300\n",
      "1/1 [==============================] - 0s 4ms/step - loss: 0.3880 - sparse_categorical_accuracy: 0.9667\n",
      "Epoch 174/300\n",
      "1/1 [==============================] - 0s 4ms/step - loss: 0.3877 - sparse_categorical_accuracy: 0.9083\n",
      "Epoch 175/300\n",
      "1/1 [==============================] - 0s 3ms/step - loss: 0.3870 - sparse_categorical_accuracy: 0.9667\n",
      "Epoch 176/300\n",
      "1/1 [==============================] - 0s 4ms/step - loss: 0.3869 - sparse_categorical_accuracy: 0.9083\n",
      "Epoch 177/300\n",
      "1/1 [==============================] - 0s 3ms/step - loss: 0.3863 - sparse_categorical_accuracy: 0.9667\n",
      "Epoch 178/300\n",
      "1/1 [==============================] - 0s 4ms/step - loss: 0.3862 - sparse_categorical_accuracy: 0.9083\n",
      "Epoch 179/300\n",
      "1/1 [==============================] - 0s 3ms/step - loss: 0.3857 - sparse_categorical_accuracy: 0.9667\n",
      "Epoch 180/300\n",
      "1/1 [==============================] - 0s 27ms/step - loss: 0.3855 - sparse_categorical_accuracy: 0.9083 - val_loss: 0.3985 - val_sparse_categorical_accuracy: 1.0000\n",
      "Epoch 181/300\n",
      "1/1 [==============================] - 0s 3ms/step - loss: 0.3851 - sparse_categorical_accuracy: 0.9667\n",
      "Epoch 182/300\n",
      "1/1 [==============================] - 0s 6ms/step - loss: 0.3849 - sparse_categorical_accuracy: 0.9083\n",
      "Epoch 183/300\n",
      "1/1 [==============================] - 0s 5ms/step - loss: 0.3844 - sparse_categorical_accuracy: 0.9667\n",
      "Epoch 184/300\n",
      "1/1 [==============================] - 0s 4ms/step - loss: 0.3841 - sparse_categorical_accuracy: 0.9167\n",
      "Epoch 185/300\n",
      "1/1 [==============================] - 0s 4ms/step - loss: 0.3837 - sparse_categorical_accuracy: 0.9667\n",
      "Epoch 186/300\n",
      "1/1 [==============================] - 0s 3ms/step - loss: 0.3835 - sparse_categorical_accuracy: 0.9167\n",
      "Epoch 187/300\n",
      "1/1 [==============================] - 0s 4ms/step - loss: 0.3831 - sparse_categorical_accuracy: 0.9667\n",
      "Epoch 188/300\n",
      "1/1 [==============================] - 0s 3ms/step - loss: 0.3829 - sparse_categorical_accuracy: 0.9250\n",
      "Epoch 189/300\n",
      "1/1 [==============================] - 0s 4ms/step - loss: 0.3825 - sparse_categorical_accuracy: 0.9667\n",
      "Epoch 190/300\n",
      "1/1 [==============================] - 0s 27ms/step - loss: 0.3822 - sparse_categorical_accuracy: 0.9250 - val_loss: 0.4019 - val_sparse_categorical_accuracy: 1.0000\n",
      "Epoch 191/300\n",
      "1/1 [==============================] - 0s 5ms/step - loss: 0.3819 - sparse_categorical_accuracy: 0.9667\n",
      "Epoch 192/300\n",
      "1/1 [==============================] - 0s 3ms/step - loss: 0.3817 - sparse_categorical_accuracy: 0.9417\n",
      "Epoch 193/300\n",
      "1/1 [==============================] - 0s 4ms/step - loss: 0.3815 - sparse_categorical_accuracy: 0.9583\n",
      "Epoch 194/300\n",
      "1/1 [==============================] - 0s 4ms/step - loss: 0.3813 - sparse_categorical_accuracy: 0.9500\n",
      "Epoch 195/300\n",
      "1/1 [==============================] - 0s 3ms/step - loss: 0.3810 - sparse_categorical_accuracy: 0.9583\n",
      "Epoch 196/300\n",
      "1/1 [==============================] - 0s 4ms/step - loss: 0.3807 - sparse_categorical_accuracy: 0.9500\n",
      "Epoch 197/300\n",
      "1/1 [==============================] - 0s 4ms/step - loss: 0.3805 - sparse_categorical_accuracy: 0.9583\n",
      "Epoch 198/300\n",
      "1/1 [==============================] - 0s 3ms/step - loss: 0.3803 - sparse_categorical_accuracy: 0.9583\n",
      "Epoch 199/300\n",
      "1/1 [==============================] - 0s 4ms/step - loss: 0.3801 - sparse_categorical_accuracy: 0.9583\n",
      "Epoch 200/300\n",
      "1/1 [==============================] - 0s 30ms/step - loss: 0.3800 - sparse_categorical_accuracy: 0.9583 - val_loss: 0.4032 - val_sparse_categorical_accuracy: 1.0000\n",
      "Epoch 201/300\n",
      "1/1 [==============================] - 0s 4ms/step - loss: 0.3798 - sparse_categorical_accuracy: 0.9583\n",
      "Epoch 202/300\n",
      "1/1 [==============================] - 0s 3ms/step - loss: 0.3797 - sparse_categorical_accuracy: 0.9583\n",
      "Epoch 203/300\n",
      "1/1 [==============================] - 0s 4ms/step - loss: 0.3794 - sparse_categorical_accuracy: 0.9583\n",
      "Epoch 204/300\n",
      "1/1 [==============================] - 0s 3ms/step - loss: 0.3793 - sparse_categorical_accuracy: 0.9583\n",
      "Epoch 205/300\n",
      "1/1 [==============================] - 0s 3ms/step - loss: 0.3791 - sparse_categorical_accuracy: 0.9583\n",
      "Epoch 206/300\n",
      "1/1 [==============================] - 0s 5ms/step - loss: 0.3789 - sparse_categorical_accuracy: 0.9583\n",
      "Epoch 207/300\n",
      "1/1 [==============================] - 0s 4ms/step - loss: 0.3788 - sparse_categorical_accuracy: 0.9583\n",
      "Epoch 208/300\n",
      "1/1 [==============================] - 0s 3ms/step - loss: 0.3786 - sparse_categorical_accuracy: 0.9583\n",
      "Epoch 209/300\n",
      "1/1 [==============================] - 0s 3ms/step - loss: 0.3785 - sparse_categorical_accuracy: 0.9583\n",
      "Epoch 210/300\n",
      "1/1 [==============================] - 0s 27ms/step - loss: 0.3783 - sparse_categorical_accuracy: 0.9583 - val_loss: 0.4036 - val_sparse_categorical_accuracy: 1.0000\n",
      "Epoch 211/300\n",
      "1/1 [==============================] - 0s 3ms/step - loss: 0.3781 - sparse_categorical_accuracy: 0.9583\n",
      "Epoch 212/300\n",
      "1/1 [==============================] - 0s 4ms/step - loss: 0.3779 - sparse_categorical_accuracy: 0.9583\n",
      "Epoch 213/300\n",
      "1/1 [==============================] - 0s 4ms/step - loss: 0.3778 - sparse_categorical_accuracy: 0.9583\n",
      "Epoch 214/300\n",
      "1/1 [==============================] - 0s 3ms/step - loss: 0.3776 - sparse_categorical_accuracy: 0.9583\n",
      "Epoch 215/300\n",
      "1/1 [==============================] - 0s 4ms/step - loss: 0.3775 - sparse_categorical_accuracy: 0.9583\n",
      "Epoch 216/300\n",
      "1/1 [==============================] - 0s 4ms/step - loss: 0.3773 - sparse_categorical_accuracy: 0.9583\n",
      "Epoch 217/300\n",
      "1/1 [==============================] - 0s 3ms/step - loss: 0.3772 - sparse_categorical_accuracy: 0.9583\n",
      "Epoch 218/300\n",
      "1/1 [==============================] - 0s 4ms/step - loss: 0.3770 - sparse_categorical_accuracy: 0.9583\n",
      "Epoch 219/300\n",
      "1/1 [==============================] - 0s 4ms/step - loss: 0.3769 - sparse_categorical_accuracy: 0.9583\n",
      "Epoch 220/300\n",
      "1/1 [==============================] - 0s 26ms/step - loss: 0.3768 - sparse_categorical_accuracy: 0.9583 - val_loss: 0.4033 - val_sparse_categorical_accuracy: 1.0000\n",
      "Epoch 221/300\n",
      "1/1 [==============================] - 0s 4ms/step - loss: 0.3766 - sparse_categorical_accuracy: 0.9583\n",
      "Epoch 222/300\n",
      "1/1 [==============================] - 0s 4ms/step - loss: 0.3765 - sparse_categorical_accuracy: 0.9583\n",
      "Epoch 223/300\n",
      "1/1 [==============================] - 0s 4ms/step - loss: 0.3764 - sparse_categorical_accuracy: 0.9583\n",
      "Epoch 224/300\n",
      "1/1 [==============================] - 0s 3ms/step - loss: 0.3762 - sparse_categorical_accuracy: 0.9583\n",
      "Epoch 225/300\n",
      "1/1 [==============================] - 0s 5ms/step - loss: 0.3761 - sparse_categorical_accuracy: 0.9583\n",
      "Epoch 226/300\n",
      "1/1 [==============================] - 0s 3ms/step - loss: 0.3759 - sparse_categorical_accuracy: 0.9583\n",
      "Epoch 227/300\n",
      "1/1 [==============================] - 0s 4ms/step - loss: 0.3758 - sparse_categorical_accuracy: 0.9583\n",
      "Epoch 228/300\n",
      "1/1 [==============================] - 0s 4ms/step - loss: 0.3756 - sparse_categorical_accuracy: 0.9583\n",
      "Epoch 229/300\n",
      "1/1 [==============================] - 0s 3ms/step - loss: 0.3755 - sparse_categorical_accuracy: 0.9583\n",
      "Epoch 230/300\n",
      "1/1 [==============================] - 0s 26ms/step - loss: 0.3754 - sparse_categorical_accuracy: 0.9583 - val_loss: 0.4023 - val_sparse_categorical_accuracy: 1.0000\n",
      "Epoch 231/300\n",
      "1/1 [==============================] - 0s 5ms/step - loss: 0.3753 - sparse_categorical_accuracy: 0.9583\n",
      "Epoch 232/300\n",
      "1/1 [==============================] - 0s 4ms/step - loss: 0.3752 - sparse_categorical_accuracy: 0.9583\n",
      "Epoch 233/300\n",
      "1/1 [==============================] - 0s 3ms/step - loss: 0.3750 - sparse_categorical_accuracy: 0.9583\n",
      "Epoch 234/300\n",
      "1/1 [==============================] - 0s 3ms/step - loss: 0.3749 - sparse_categorical_accuracy: 0.9583\n",
      "Epoch 235/300\n",
      "1/1 [==============================] - 0s 4ms/step - loss: 0.3748 - sparse_categorical_accuracy: 0.9583\n",
      "Epoch 236/300\n",
      "1/1 [==============================] - 0s 3ms/step - loss: 0.3747 - sparse_categorical_accuracy: 0.9583\n",
      "Epoch 237/300\n",
      "1/1 [==============================] - 0s 3ms/step - loss: 0.3745 - sparse_categorical_accuracy: 0.9583\n",
      "Epoch 238/300\n",
      "1/1 [==============================] - 0s 3ms/step - loss: 0.3744 - sparse_categorical_accuracy: 0.9583\n",
      "Epoch 239/300\n",
      "1/1 [==============================] - 0s 6ms/step - loss: 0.3743 - sparse_categorical_accuracy: 0.9583\n",
      "Epoch 240/300\n",
      "1/1 [==============================] - 0s 25ms/step - loss: 0.3741 - sparse_categorical_accuracy: 0.9583 - val_loss: 0.4027 - val_sparse_categorical_accuracy: 1.0000\n",
      "Epoch 241/300\n",
      "1/1 [==============================] - 0s 5ms/step - loss: 0.3740 - sparse_categorical_accuracy: 0.9583\n",
      "Epoch 242/300\n",
      "1/1 [==============================] - 0s 3ms/step - loss: 0.3740 - sparse_categorical_accuracy: 0.9583\n",
      "Epoch 243/300\n",
      "1/1 [==============================] - 0s 4ms/step - loss: 0.3738 - sparse_categorical_accuracy: 0.9583\n",
      "Epoch 244/300\n",
      "1/1 [==============================] - 0s 3ms/step - loss: 0.3737 - sparse_categorical_accuracy: 0.9583\n",
      "Epoch 245/300\n",
      "1/1 [==============================] - 0s 4ms/step - loss: 0.3736 - sparse_categorical_accuracy: 0.9583\n",
      "Epoch 246/300\n",
      "1/1 [==============================] - 0s 4ms/step - loss: 0.3735 - sparse_categorical_accuracy: 0.9583\n",
      "Epoch 247/300\n",
      "1/1 [==============================] - 0s 3ms/step - loss: 0.3734 - sparse_categorical_accuracy: 0.9583\n",
      "Epoch 248/300\n",
      "1/1 [==============================] - 0s 6ms/step - loss: 0.3732 - sparse_categorical_accuracy: 0.9583\n",
      "Epoch 249/300\n",
      "1/1 [==============================] - 0s 3ms/step - loss: 0.3731 - sparse_categorical_accuracy: 0.9583\n",
      "Epoch 250/300\n",
      "1/1 [==============================] - 0s 27ms/step - loss: 0.3730 - sparse_categorical_accuracy: 0.9583 - val_loss: 0.4010 - val_sparse_categorical_accuracy: 1.0000\n",
      "Epoch 251/300\n",
      "1/1 [==============================] - 0s 3ms/step - loss: 0.3729 - sparse_categorical_accuracy: 0.9583\n",
      "Epoch 252/300\n",
      "1/1 [==============================] - 0s 5ms/step - loss: 0.3728 - sparse_categorical_accuracy: 0.9583\n",
      "Epoch 253/300\n",
      "1/1 [==============================] - 0s 4ms/step - loss: 0.3727 - sparse_categorical_accuracy: 0.9583\n",
      "Epoch 254/300\n",
      "1/1 [==============================] - 0s 4ms/step - loss: 0.3726 - sparse_categorical_accuracy: 0.9583\n",
      "Epoch 255/300\n",
      "1/1 [==============================] - 0s 3ms/step - loss: 0.3725 - sparse_categorical_accuracy: 0.9583\n",
      "Epoch 256/300\n",
      "1/1 [==============================] - 0s 4ms/step - loss: 0.3724 - sparse_categorical_accuracy: 0.9583\n",
      "Epoch 257/300\n",
      "1/1 [==============================] - 0s 4ms/step - loss: 0.3723 - sparse_categorical_accuracy: 0.9583\n",
      "Epoch 258/300\n",
      "1/1 [==============================] - 0s 3ms/step - loss: 0.3721 - sparse_categorical_accuracy: 0.9583\n",
      "Epoch 259/300\n",
      "1/1 [==============================] - 0s 3ms/step - loss: 0.3721 - sparse_categorical_accuracy: 0.9583\n",
      "Epoch 260/300\n",
      "1/1 [==============================] - 0s 25ms/step - loss: 0.3720 - sparse_categorical_accuracy: 0.9583 - val_loss: 0.3993 - val_sparse_categorical_accuracy: 1.0000\n",
      "Epoch 261/300\n",
      "1/1 [==============================] - 0s 4ms/step - loss: 0.3719 - sparse_categorical_accuracy: 0.9583\n",
      "Epoch 262/300\n",
      "1/1 [==============================] - 0s 2ms/step - loss: 0.3718 - sparse_categorical_accuracy: 0.9583\n",
      "Epoch 263/300\n",
      "1/1 [==============================] - 0s 4ms/step - loss: 0.3716 - sparse_categorical_accuracy: 0.9583\n",
      "Epoch 264/300\n",
      "1/1 [==============================] - 0s 3ms/step - loss: 0.3716 - sparse_categorical_accuracy: 0.9583\n",
      "Epoch 265/300\n",
      "1/1 [==============================] - 0s 3ms/step - loss: 0.3715 - sparse_categorical_accuracy: 0.9583\n",
      "Epoch 266/300\n",
      "1/1 [==============================] - 0s 3ms/step - loss: 0.3714 - sparse_categorical_accuracy: 0.9583\n",
      "Epoch 267/300\n",
      "1/1 [==============================] - 0s 3ms/step - loss: 0.3713 - sparse_categorical_accuracy: 0.9583\n",
      "Epoch 268/300\n",
      "1/1 [==============================] - 0s 3ms/step - loss: 0.3712 - sparse_categorical_accuracy: 0.9583\n",
      "Epoch 269/300\n",
      "1/1 [==============================] - 0s 4ms/step - loss: 0.3710 - sparse_categorical_accuracy: 0.9583\n",
      "Epoch 270/300\n",
      "1/1 [==============================] - 0s 26ms/step - loss: 0.3709 - sparse_categorical_accuracy: 0.9583 - val_loss: 0.3980 - val_sparse_categorical_accuracy: 1.0000\n",
      "Epoch 271/300\n",
      "1/1 [==============================] - 0s 3ms/step - loss: 0.3708 - sparse_categorical_accuracy: 0.9583\n",
      "Epoch 272/300\n",
      "1/1 [==============================] - 0s 3ms/step - loss: 0.3708 - sparse_categorical_accuracy: 0.9583\n",
      "Epoch 273/300\n",
      "1/1 [==============================] - 0s 4ms/step - loss: 0.3707 - sparse_categorical_accuracy: 0.9583\n",
      "Epoch 274/300\n",
      "1/1 [==============================] - 0s 3ms/step - loss: 0.3706 - sparse_categorical_accuracy: 0.9583\n",
      "Epoch 275/300\n",
      "1/1 [==============================] - 0s 4ms/step - loss: 0.3705 - sparse_categorical_accuracy: 0.9583\n",
      "Epoch 276/300\n",
      "1/1 [==============================] - 0s 3ms/step - loss: 0.3704 - sparse_categorical_accuracy: 0.9583\n",
      "Epoch 277/300\n",
      "1/1 [==============================] - 0s 3ms/step - loss: 0.3703 - sparse_categorical_accuracy: 0.9583\n",
      "Epoch 278/300\n",
      "1/1 [==============================] - 0s 4ms/step - loss: 0.3702 - sparse_categorical_accuracy: 0.9583\n",
      "Epoch 279/300\n",
      "1/1 [==============================] - 0s 3ms/step - loss: 0.3701 - sparse_categorical_accuracy: 0.9583\n",
      "Epoch 280/300\n",
      "1/1 [==============================] - 0s 29ms/step - loss: 0.3700 - sparse_categorical_accuracy: 0.9583 - val_loss: 0.3968 - val_sparse_categorical_accuracy: 1.0000\n",
      "Epoch 281/300\n",
      "1/1 [==============================] - 0s 4ms/step - loss: 0.3699 - sparse_categorical_accuracy: 0.9583\n",
      "Epoch 282/300\n",
      "1/1 [==============================] - 0s 3ms/step - loss: 0.3698 - sparse_categorical_accuracy: 0.9583\n",
      "Epoch 283/300\n",
      "1/1 [==============================] - 0s 5ms/step - loss: 0.3698 - sparse_categorical_accuracy: 0.9583\n",
      "Epoch 284/300\n",
      "1/1 [==============================] - 0s 4ms/step - loss: 0.3697 - sparse_categorical_accuracy: 0.9583\n",
      "Epoch 285/300\n",
      "1/1 [==============================] - 0s 3ms/step - loss: 0.3696 - sparse_categorical_accuracy: 0.9583\n",
      "Epoch 286/300\n",
      "1/1 [==============================] - 0s 4ms/step - loss: 0.3695 - sparse_categorical_accuracy: 0.9583\n",
      "Epoch 287/300\n",
      "1/1 [==============================] - 0s 3ms/step - loss: 0.3694 - sparse_categorical_accuracy: 0.9583\n",
      "Epoch 288/300\n",
      "1/1 [==============================] - 0s 4ms/step - loss: 0.3693 - sparse_categorical_accuracy: 0.9583\n",
      "Epoch 289/300\n",
      "1/1 [==============================] - 0s 3ms/step - loss: 0.3692 - sparse_categorical_accuracy: 0.9583\n",
      "Epoch 290/300\n",
      "1/1 [==============================] - 0s 26ms/step - loss: 0.3691 - sparse_categorical_accuracy: 0.9583 - val_loss: 0.3967 - val_sparse_categorical_accuracy: 1.0000\n",
      "Epoch 291/300\n",
      "1/1 [==============================] - 0s 5ms/step - loss: 0.3691 - sparse_categorical_accuracy: 0.9583\n",
      "Epoch 292/300\n",
      "1/1 [==============================] - 0s 3ms/step - loss: 0.3690 - sparse_categorical_accuracy: 0.9583\n",
      "Epoch 293/300\n",
      "1/1 [==============================] - 0s 3ms/step - loss: 0.3689 - sparse_categorical_accuracy: 0.9583\n",
      "Epoch 294/300\n",
      "1/1 [==============================] - 0s 2ms/step - loss: 0.3688 - sparse_categorical_accuracy: 0.9583\n",
      "Epoch 295/300\n",
      "1/1 [==============================] - 0s 4ms/step - loss: 0.3687 - sparse_categorical_accuracy: 0.9583\n",
      "Epoch 296/300\n",
      "1/1 [==============================] - 0s 3ms/step - loss: 0.3686 - sparse_categorical_accuracy: 0.9583\n",
      "Epoch 297/300\n",
      "1/1 [==============================] - 0s 4ms/step - loss: 0.3685 - sparse_categorical_accuracy: 0.9583\n",
      "Epoch 298/300\n",
      "1/1 [==============================] - 0s 3ms/step - loss: 0.3684 - sparse_categorical_accuracy: 0.9583\n",
      "Epoch 299/300\n",
      "1/1 [==============================] - 0s 3ms/step - loss: 0.3683 - sparse_categorical_accuracy: 0.9583\n",
      "Epoch 300/300\n",
      "1/1 [==============================] - 0s 27ms/step - loss: 0.3682 - sparse_categorical_accuracy: 0.9583 - val_loss: 0.3943 - val_sparse_categorical_accuracy: 1.0000\n",
      "Model: \"iris_model\"\n",
      "_________________________________________________________________\n",
      " Layer (type)                Output Shape              Param #   \n",
      "=================================================================\n",
      " dense (Dense)               multiple                  15        \n",
      "                                                                 \n",
      "=================================================================\n",
      "Total params: 15\n",
      "Trainable params: 15\n",
      "Non-trainable params: 0\n",
      "_________________________________________________________________\n"
     ]
    }
   ],
   "source": [
    "# 1，导入相关模块\n",
    "import tensorflow as tf\n",
    "from tensorflow.keras.layers import Dense\n",
    "from tensorflow.keras import Model\n",
    "from sklearn import datasets\n",
    "import numpy as np\n",
    "\n",
    "# 2，指定输入网络的训练集\n",
    "x_train = datasets.load_iris().data\n",
    "y_train = datasets.load_iris().target\n",
    "\n",
    "# 随机打乱数据\n",
    "np.random.seed(116)  \n",
    "np.random.shuffle(x_train)\n",
    "np.random.seed(116)\n",
    "np.random.shuffle(y_train)\n",
    "\n",
    "# 3，自定义网络结构\n",
    "class IrisModel(Model):\n",
    "    def __init__(self):\n",
    "        super(IrisModel,self).__init__()\n",
    "        self.d1=Dense(\n",
    "            units=3,\n",
    "            activation='softmax',\n",
    "            kernel_regularizer=tf.keras.regularizers.l2()\n",
    "        )\n",
    "    def call(self,x):        \n",
    "        y=self.d1(x)\n",
    "        return y\n",
    "\n",
    "# 实例化Model对象\n",
    "model=IrisModel()\n",
    "\n",
    "# 4，配置训练方法\n",
    "model.compile(\n",
    "    optimizer=tf.keras.optimizers.SGD(learning_rate=0.1),\n",
    "    loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits= False),\n",
    "    metrics=['sparse_categorical_accuracy']\n",
    ")\n",
    "\n",
    "# 5，执行训练过程\n",
    "model.fit(\n",
    "    x_train,\n",
    "    y_train,\n",
    "    batch_size=128,\n",
    "    epochs=300,\n",
    "    validation_split=0.2, \n",
    "    validation_freq=10,\n",
    "    verbose=1\n",
    ")\n",
    "\n",
    "# 6，打印网络结构和统计参数数据\n",
    "model.summary()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e3b3e36e-8418-4caf-af51-d8ac80e2a321",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
